<!--
 * @Description:
 * @version:
 * @Author: sunqian_sr
 * @Date: 2022-11-29 16:36:58
 * @LastEditors: Andy
 * @LastEditTime: 2023-01-13 18:40:56
-->
<template>
  <x-dialog
    :title="title"
    :show.sync="visible"
    v-if="visible"
    :before-close="handleClose"
    width="40%"
    @onCancel="handleClose"
    @onSubmit="handleSubmit"
  >
    <el-form
      :model="params"
      class="demo-form-inline"
      ref="params"
      :rules="rules"
      label-position="top"
      label-width="auto"
    >
      <el-row :gutter="36">
        <el-col :span="24">
          <el-form-item :label="$t('system.dictValue.dictValueName')" prop="dictLabel">
            <el-input
              v-model="params.dictLabel"
              :placeholder="$t('system.dictValue.dictValueNamePlaceholder')"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('system.dictValue.dictValueCode')" prop="dictValue">
            <el-input
              v-model="params.dictValue"
              :placeholder="$t('system.dictValue.dictValueCodePlaceholder')"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('system.dictValue.International')" prop="lang">
            <x-select
              :placeholder="$t('system.user.pleaseSelect')"
              v-model="params.lang"
              :options="dictOptions['system.lang']"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('system.dictValue.dictSort')" prop="sort">
            <el-input-number
              v-model="params.sort"
              :placeholder="$t('system.dictValue.dictSortPlaceholder')"
            ></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('system.dict.enable')" prop="state">
            <x-radio-group :listData="dictOptions['system.state']" v-model="params.state"></x-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('system.dict.defaultValue')" prop="preset">
            <x-radio-group :listData="dictOptions['system.preset']" v-model="params.preset"></x-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </x-dialog>
</template>
<script>
  import { throttle, cloneDeep, omit } from '@package/utils'
  import { apiPostDictItemAdd, apiPutDictItemUpdate } from '../api/dictValue.js'
  import { EnumApiCode } from '../../../../../../globalEnum.js'

  export default {
    props: {
      dictOptions: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      return {
        visible: false,
        title: this.$t('system.dictValue.add'),
        params: {
          dictLabel: '',
          dictValue: '',
          state: '1',
          lang: '',
          sort: 0,
          preset: '1'
        },
        rules: {
          dictLabel: [
            { required: true, message: this.$t('system.dictValue.dictValueNamePlaceholder'), trigger: 'blur' },
            { min: 1, max: 100, trigger: 'blur' }
          ],
          dictValue: [
            { required: true, message: this.$t('system.dictValue.dictValueCodePlaceholder'), trigger: 'blur' },
            { min: 1, max: 100, trigger: 'blur' }
          ],
          lang: [{ required: true, message: this.$t('system.dictValue.InternationalPlaceholder'), trigger: 'blur' }],
          sort: [{ required: true, message: this.$t('system.dictValue.dictSortPlaceholder'), trigger: 'blur' }],
          preset: [{ required: true, message: this.$t('system.dict.enable'), trigger: 'blur' }],
          state: [{ required: true, message: this.$t('system.dict.enable'), trigger: 'blur' }]
        }
      }
    },
    computed: {
      dictId() {
        return this.$route.query.dictId
      }
    },
    methods: {
      handleOpen(info) {
        this.visible = true
        if (info?.id) {
          this.$nextTick(() => {
            this.params = Object.assign(this.params, cloneDeep(info))
            this.params.state += ''
            this.params.preset += ''
          })
          this.title = this.$t('common.edit')
        } else {
          this.title = this.$t('common.add')
        }
      },
      handleClose() {
        this.$refs.params.resetFields()
        this.params = omit(this.params, 'id')
        this.visible = false
      },
      handleSubmit: throttle(function () {
        this.$refs.params.validate(async (valid) => {
          if (valid) {
            const param = {
              ...this.params,
              dictId: this.dictId
            }
            const { code, msg } = param.id ? await apiPutDictItemUpdate(param) : await apiPostDictItemAdd(param)
            if (code === EnumApiCode.Success) {
              param.id
                ? this.$message.success(this.$t('common.editSuccess'))
                : this.$message.success(this.$t('common.newSuccess'))
              this.$emit('handleSuccess')
              this.handleClose()
            } else {
              this.$message.error(msg)
            }
          } else {
            return false
          }
        })
      }, 1000)
    }
  }
</script>
